package com.socket.tools.controller;

import com.socket.tools.service.ConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Web控制器
 * 提供页面路由和API接口
 */
@Controller
public class WebController {
    
    @Autowired
    private ConnectionManager connectionManager;
    
    /**
     * 客户端页面
     */
    @GetMapping("/client")
    public String clientPage() {
        return "client";
    }
    
    /**
     * 服务端管理页面
     */
    @GetMapping("/admin")
    public String adminPage() {
        return "admin";
    }
    
    /**
     * 首页
     */
    @GetMapping("/")
    public String index() {
        return "index";
    }
    
    /**
     * 获取连接队列信息
     */
    @GetMapping("/api/queue")
    @ResponseBody
    public Map<String, Object> getQueueInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("queue", connectionManager.getConnectionQueue());
        map.put("activeConnections", connectionManager.getActiveConnections());
        map.put("statistics", connectionManager.getStatistics());
        return map;
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/api/stats")
    @ResponseBody
    public Map<String, Object> getStatistics() {
        return connectionManager.getStatistics();
    }
    
    /**
     * 获取服务端用户列表
     */
    @GetMapping("/api/admins")
    @ResponseBody
    public Map<String, Object> getAdminList() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("admins", connectionManager.getAdminList());
        return map;
    }
} 